<?php

//require './php_users.php';

$roles = procedure_call0("ROLESSISTEMA");
$roles_ = Array("DEFAULT");
$privilegios = procedure_call0("PRIVSISTEMA");
$privilegios_ = Array("DEFAULT");
$Temporal= procedure_call0("TBSTemporales");
$Temporales = Array("DEFAULT");
$tablespaces = procedure_call0("TABLESPACES"); 
$tablespaces_ = Array("DEFAULT");

//$tablas = procedure_call("TODASTABLAS"); 
//$tablas_ = Array();

foreach($Temporal as $r)
        array_push($Temporales, implode($r));
foreach ($roles as $r)
    array_push($roles_, implode($r));
foreach ($privilegios as $p)
    array_push($privilegios_, implode($p));
    foreach($tablespaces as $t)
    array_push($tablespaces_, implode($t));
//    foreach($tablas as $t0)
//    { 
//        $tv = implode($t0);
//        array_push($privilegios_,"SELECT ON ".$tv);
//        array_push($privilegios_,"DELETE ON ".$tv);
//        array_push($privilegios_,"INSERT ON ".$tv);
//        array_push($privilegios_,"UPDATE ON ".$tv);
//        array_push($privilegios_,"ALL ON ".$tv);
//    }


$str = '<div id="divPopUsuarios" class="divScroll">';

$str.= '<div class="row">' . selectBox00(Array("DEFAULT","nivel I", "nivel II", "nivel III", "nivel IV"), "seguridadselect0", "Seguridad de Clave", "col-md-2");
$str.= inputBonito0("Clave0", "Inserte una Clave", "password", "col-md-4");
$str.= inputBonito0("Clave10", "Confirme su Clave", "password", "col-md-4");
$str.= selectBox00(Array("DEFAULT","30 días", "60 días", "90 días", "∞ días"), "caducaselect0", "Caducidad de Clave", "col-md-2") . "</div>";

$str.= '<div class="row">' . selectBox00($tablespaces_, "cmbDefaultTBS0", "DEFAULT_TABLESPACE", "col-md-6");
$str .= selectBox00($Temporales, "cmbTemporaryTBS0", "TEMPORARY TABLESPACE", "col-md-6") . '</div>';

//CUOTA
//$str .= '<div class="row">' . selectBox00(ARRAY(), "cmbobjetos0", "Objetos del Usuario", "col-md-5");
//$str .= selectBox00(ARRAY(), "cmbcuotas0", "Cuotas por Tablespace", "col-md-3");
$str .= '<div class="col-md-4">' . label_checkbox(ARRAY(), "", "checkAct", "ACTIVAR/DESACTIVAR USUARIO") . '</div>' . '</div>';

//--aqui va las tablas
$str.= '<div class="row">';
$str.= '<div class="form-group col-md-6"><div class="row">';
$str.= selectBox00($roles_, "cmbRoles0", "ROLES", "col-md-6") . '<span class="glyphicon glyphicon-plus-sign col-md-1" onclick="addNuevaRow(' . "'cmbRoles0'" . ')"></span></div>';
$str.= Tabla0(Array("Row", "Role"), Array(), "", "tablarolPop");
$str.= '</div>';
$str.= '<div class="form-group col-md-6"><div class="row">';
$str.= selectBox00($privilegios_, "cmbPermisos0", "Permisos", "col-md-6") . '<span class="glyphicon glyphicon-plus-sign col-md-1" onclick="addNuevaRow(' . "'cmbPermisos0'" . ')"></span></div>';
$str.= Tabla0(Array("Row", "Privilege"), Array(), "", "tablaprivPop");
$str.= '</div></div>';
$str.= '</div>';
echo $str;

function label_checkbox($checkboxes, $nomCheck, $idCheck, $labelName) {
    $str = '<div class="row-fluid">';
    $str .= Label($labelName);
    //foreach ($checkboxes as $value) {
    $str.= '<div class="checkbox">';
    $str .= '<label><input id="' . $idCheck . '" type="checkbox" value="Activar">Activar</label>';
    $str .= '<label><input id="' . $idCheck . '2" type="checkbox" value="Desactivar">Desactivar</label>';
    $str .='   </div>';
    //   }   
    $str .= '</div>';

    return $str;
}

function Label0($valor) {
    $str = '<span class="label label-primary">' . $valor . '</span>';
    return $str;
}

function Label($valor) {
    $str = '<span class="label label-primary">' . $valor . '</span>';
    return $str;
}

function selectBox00($opciones, $id, $labelName, $class) {
    $str = '<div class="form-group ' . $class . '">';
    $str .= '<div class="row-fluid" id="'.$id.'_div">';
    $str .= Label0($labelName);
    $str .= '<select class="selectpicker" id="' . $id . '" data-style="btn-primary">';
    foreach ($opciones as $value)
        $str .= '<option value="'.$value.'">' . $value . '</option>';
    $str.= '</select>';
    $str .= '</div></div>';
    return $str;
}

function inputBonito0($name, $des, $type, $class) {
    $str = '<div class="form-group ' . $class . '">';
    $str .= '<div class="row-fluid">';
    $str .= Label($name);
    $str.= '<input type="' . $type . '"placeholder="' . $des . '" id="id_' . $name . '">';
    $str.='</div></div>';
    return $str;
}

function Tabla0($theads, $tbodys, $onclick, $id) {
    $cantCol = 0;
    $cantIns = 0;

    $str = '<div class="table-responsive"> ';
    $str .= '<table class="table table-striped table-fixed-header" id="' . $id . '">'; //  table table-bordered table-fixed-header
    $str.= ' <thead class="header">';

    //lleno thead
    $resultado = count($theads);
    $bool = true;
    foreach ($theads as $thead) {
        if ($bool) {
            $str.= '<tr>';
            $bool = false;
        }
        $str.='<th>' . $thead . '</th>';
        $cantCol++;
        if ($cantCol == $resultado) {
            $str.='</tr>';
        }
    }
    $str.='</thead>';

    //lleno tbody
    $str.='<tbody>';
    foreach ($tbodys as $tbody) {
        if ($cantIns == 0) {
            $cantIns++;
            $str.='<tr onclick="' . $onclick . '" >';
            $str.='<td>' . $tbody . '</td>';
        } elseif ($cantIns != $cantCol && $cantIns <= $cantCol) {
            $cantIns++;
            $str.='<td>' . $tbody . '</td>';
            if ($cantIns == $cantCol) {
                $str.='</tr>';
                $cantIns = 0;
            }
        }
    }
    $str.='</tbody>';
    $str.='</table>';
    $str.='</div>';
    return $str;
//$divPrincipal->echo ($str);
}

function procedure_call0($pr) {
    require("./Conn.php");
    $array = Array();
    if (!$conn) {
        $m = oci_error();
        trigger_error(htmlentities($m['message']), E_USER_ERROR);
    }
    $curs = oci_new_cursor($conn);
    $stid = oci_parse($conn, "begin " . $pr . "(:cursorAux); end;");
    //$stid = oci_parse($conn, "begin PRC001(:cursorAux); end;");
    oci_bind_by_name($stid, ":cursorAux", $curs, -1, OCI_B_CURSOR);
    oci_execute($stid);
    oci_execute($curs);  // Execute the REF CURSOR like a normal statement id 
    while ($row = oci_fetch_array($curs, OCI_ASSOC)) {
        array_push($array, $row);
    }
    return $array;
}
